package cn.indi.zyj.member.service;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.indi.zyj.common.exception.BusinessException;
import cn.indi.zyj.common.exception.BusinessExceptionEnum;
import cn.indi.zyj.common.util.JwtUtil;
import cn.indi.zyj.common.util.SnowUtil;
import cn.indi.zyj.member.domain.Member;
import cn.indi.zyj.member.domain.MemberExample;
import cn.indi.zyj.member.mapper.MemberMapper;
import cn.indi.zyj.member.req.MemberLoginReq;
import cn.indi.zyj.member.req.MemberRegisterReq;
import cn.indi.zyj.member.req.MemberSendCodeReq;
import cn.indi.zyj.member.resp.MemberLoginResp;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MemberService {


    private static final Logger LOG = LoggerFactory.getLogger(MemberService.class);


    @Resource
    private MemberMapper memberMapper;

    public int count(){
        return Math.toIntExact(memberMapper.countByExample(null));
    }



    public long register(MemberRegisterReq registerReq){

        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andMobileEqualTo(registerReq.getMobile());
        List<Member> members = memberMapper.selectByExample(memberExample);

        if (CollUtil.isNotEmpty(members)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
        }

        Member member = new Member();
        member.setId(System.currentTimeMillis());
        member.setMobile(registerReq.getMobile());

        memberMapper.insert(member);

        return member.getId();
    }

    public void memberSendCode(MemberSendCodeReq memberSendCodeReq){

        String mobile = memberSendCodeReq.getMobile();

        Member memberForDB = selectMemberByMobile(mobile);

        if (ObjectUtil.isNull(memberForDB)){
            LOG.info("手机号不存在，插入数据！");
            Member member = new Member();
            member.setId(SnowUtil.getSnowflakeNextId());
            member.setMobile(mobile);
            memberMapper.insert(member);
        }else {
            LOG.info("手机号已存在，请重新输入！");
        }

        // 生成验证码
        // String code = RandomUtil.randomString(4);
        String code = "666666";
        LOG.info("生成短信验证码：{}", code);

        // 保存短信记录表：手机号，短信验证码，有效期，是否已使用，业务类型，发送时间，使用时间
        LOG.info("保存短信记录表");

        // 对接短信通道，发送短信
        LOG.info("对接短信通道");

    }

    public MemberLoginResp login(MemberLoginReq memberLoginReq){

        String mobile = memberLoginReq.getMobile();
        String code = memberLoginReq.getCode();

        Member memberForDB = selectMemberByMobile(mobile);

        if (ObjectUtil.isNull(memberForDB)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
        }

        if (!"666666".equals(code)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
        }

        MemberLoginResp memberLoginResp = BeanUtil.copyProperties(memberForDB, MemberLoginResp.class);
        String token = JwtUtil.createToken(memberLoginResp.getId(), memberLoginResp.getMobile());
        memberLoginResp.setCode(token);

        return memberLoginResp;



    }

    private Member selectMemberByMobile(String mobile){
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andMobileEqualTo(mobile);
        List<Member> members = memberMapper.selectByExample(memberExample);
        if (CollUtil.isEmpty(members)){
            return null;
        }else {
            return members.get(0);
        }
    }



}
